<?php

class Admin_Model_Resource extends Zend_Db_Table_Abstract
{
    protected $_name = 'resource';
    public function getAllData(){
        $query = $this->select()
                        ->from($this->_name);
        return $this->fetchAll($query);
    }
    public function getDataByResourceId($data){
        $query = $this->select()
                        ->setIntegrityCheck(false)
                        ->from($this->_name)
                        ->where('resourceId = ?',(int)$data);
        return $this->fetchRow($query);
    }
    public function getData($data){
        $query = $this->select()
                        ->from($this->_name);
        if(isset($data['moduleName']) && $data['moduleName']){
            $query->where('moduleName = ?',$data['moduleName']);
        }
        if(isset($data['controllerName']) && $data['controllerName']){
            $query->where('controllerName = ?',$data['controllerName']);
        }
        if(isset($data['actionName']) && $data['actionName']){
            $query->where('actionName = ?',$data['actionName']);
        }
        if(isset($data['resourceList']) && $data['resourceList']){
            $query->where('resourceId IN (?)',$data['resourceList']);
        }
        return $this->fetchAll($query);
    }
    public function getDataCheckUnique($data){
        $query = $this->select()
                        ->setIntegrityCheck(false)
                        ->from($this->_name);
        if(isset($data['moduleName']) && $data['moduleName']){
            $query->where("moduleName = '$data[moduleName]'");
        }
        if(isset($data['controllerName']) && $data['controllerName']){
            $query->where("controllerName = '$data[controllerName]'");
        }
        if(isset($data['actionName']) && $data['actionName']){
            $query->where("actionName = '$data[actionName]'");
        }
        return $this->fetchRow($query);
    }
    public function addData($data){
        $data = array('resourceName'=> $data['resourceName'],
                'moduleName'        => $data['moduleName'],
                'controllerName'    => $data['controllerName'],
                'actionName'        => $data['actionName'],
                'menu'              => $data['menu'],
                'detail'            => $data['detail'],
                'ord'               => $data['ord']);
        $idAdd = $this->insert($data);
        return $idAdd;
    }
    public function updateData($resourceId = '',$data){
        $data = array('resourceName'=> $data['resourceName'],
                'moduleName'        => $data['moduleName'],
                'controllerName'    => $data['controllerName'],
                'actionName'        => $data['actionName'],
                'menu'              => $data['menu'],
                'detail'            => $data['detail'],
                'ord'               => $data['ord']);
        return $this->update($data, 'resourceId = '.(int)$resourceId);
    }
    public function deleteData($id){
        $this->delete('resourceId = '.(int)$id);
    }
}